package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.VO.MapVO;
import com.itheima.dao.memberMapper;
import com.itheima.dao.orderMapper;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import javax.xml.crypto.Data;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private orderMapper orderMapper;
    @Autowired
    private memberMapper memberMapper;

    public Map<String, Object> getBusinessReport() throws Exception {
        Date date = new Date();

        String reportDate = DateUtils.parseDate2String(date);   //日期

        Integer todayVisitsNumber=orderMapper.findOrderStatusCountByDate(reportDate); //当日到诊数

       Integer todayNewMember= memberMapper.findCountByRegTime(reportDate);    //新增会员数

        //获取当前日期的 本周第一天和本周 最后一天
        //保存本周 开始结束日期 字符串格式的map集合
        HashMap<String, String> hashMap = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        //得到本周的第几天
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        //换成中国人的 一周的第几天
        i=i-1;
        //得到本周的开始日期
        calendar.add(Calendar.DATE,-(i-1));
        String startDay = DateUtils.parseDate2String(calendar.getTime());
        hashMap.put("startDay",startDay);
        //得到本周的结束日期
        calendar.add(Calendar.DATE,+6);
        String endDay = DateUtils.parseDate2String(calendar.getTime());
        hashMap.put("endDay",endDay);
        Integer thisWeekVisitsNumber= orderMapper.findStatusByWeek(hashMap); //本周到诊数
        //本月的 2019-12 String
        String yueDate = new SimpleDateFormat("yyyy-MM").format(new Date());
       Integer thisMonthNewMember  =   memberMapper.findCountByredtime(yueDate); //本月新增会员数

      Integer thisWeekNewMember  =   memberMapper.findCountByredtimeWeek(hashMap); //本周新增会员数

        Integer totalMember =memberMapper.findAllmemberCount(); //总会员数

        Integer thisMonthOrderNumber =orderMapper.findCountByorderDate(yueDate);  //本月预约数

        Integer thisMonthVisitsNumber =orderMapper.findStatusByMouth(yueDate);   //本月到诊数

        Integer todayOrderNumber =orderMapper.findCountByNowDate(reportDate);   //今日预约数

        Integer thisWeekOrderNumber=orderMapper.findOrderDateCountByWeek(hashMap);  //thisWeekOrderNumber

        List<Object> list = new ArrayList<>();
        List<MapVO> mapVOList=orderMapper.findhotSetmeal1();
        Integer allOrdercount =orderMapper.findAllorder();
        for (MapVO mapVO : mapVOList) {
            HashMap<String, Object> map = new HashMap<>();
            String name = mapVO.getName();
            Integer setmeal_count = mapVO.getSetmeal_count();
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            String proportion = decimalFormat.format((float) setmeal_count / allOrdercount);
            map.put("proportion",proportion);
            map.put("name",name);
            map.put("setmeal_count",setmeal_count);
            list.add(map);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("reportDate",reportDate);
        map.put("todayNewMember",todayNewMember);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("totalMember",totalMember);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("hotSetmeal",list);
        return map;
    }
}
/*
"data":{    
        "todayVisitsNumber":0,       今日到诊数
        "reportDate":"2019‐04‐25",    日期
        "todayNewMember":0,        新增会员数
        "thisWeekVisitsNumber":0,     本周到诊数
        "thisMonthNewMember":2,     本月新增会员数
        "thisWeekNewMember":0,      本周新增会员数
        "totalMember":10,            总会员数
        "thisMonthOrderNumber":2,      本月预约数
        "thisMonthVisitsNumber":0,    本月到诊数
        "todayOrderNumber":0,        今日预约数
        "thisWeekOrderNumber":0,      本周预约数
        "hotSetmeal":[              热门套餐
                {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5},      
                {"proportion":0.1818,"name":"阳光爸妈升级肿瘤12项筛查体检套餐","setmeal_count":2},      
                {"proportion":0.1818,"name":"珍爱高端升级肿瘤12项筛查","setmeal_count":2},      
                {"proportion":0.0909,"name":"孕前检查套餐","setmeal_count":1}    
        ],  
    },
*/